#include "MainPlayerPosState.h"
#include "HUDplayerPosBehaviour.h"
#include "Point.h"
#include "Transformation.h"
#include "Size.h"
#include "Scene.h"
#include "Terrain.h"

using namespace game_engine::components;
using namespace game_engine::library;
using namespace game_engine::utils;

///////////////////////////////////////////////////////////////////////////////
// Constructors and Destructors
///////////////////////////////////////////////////////////////////////////////

MainPlayerPosState::MainPlayerPosState(void)
    : State()
{
}

MainPlayerPosState::MainPlayerPosState(Behaviour *parent)
    : State(parent)
{
}

MainPlayerPosState::~MainPlayerPosState(void)
{
}

///////////////////////////////////////////////////////////////////////////////
// Other public methods
///////////////////////////////////////////////////////////////////////////////

void MainPlayerPosState::onLoop(const FLOAT32 time) throw(...)
{
    // Calculate new Y position according to subject's position in World
    // 1. Get the terrain dimensions
    GameObject *world = ((fsm->getObject())->getScene())->getObjectByName("World");
    Terrain *t = dynamic_cast<Terrain*> (world->getComponent("Terrain"));
    Size worldDim = t->getDimensions();

    // 2. Calculate relative position of subject based on terrain dimensions
    HUDplayerPosBehaviour *b = dynamic_cast<HUDplayerPosBehaviour*> (fsm);
    Point subjectPos = b->getSubjectPosition();
    FLOAT32 relativePosition = subjectPos.getY() / worldDim.getHeight();

    // 3. Translate this relative position to HUD player position widget
    FLOAT32 iconPos = (b->getIconYmax() - b->getIconYmin()) * relativePosition;
    iconPos += b->getIconYmin();
    Transformation *iconTransf = dynamic_cast<Transformation*> 
        ((fsm->getObject())->getComponent("Transformation"));
    iconTransf->setYPosition(iconPos);
}
